<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin\v1\system\admin;

use app\controller\admin\Auth;
use app\services\system\admin\SystemAdminServices;
use hdphp\services\CacheService;
use think\facade\{App, Config};

/**
 * Class：系统管理员控制器
 * 创建人：Jack
 * 创建时间：2023/7/31 11:15
 * @package app\adminapi\controller\v1\system\admin
 */
class SystemAdmin extends Auth
{
    /**
     * SystemAdmin constructor.
     * @param App                 $app
     * @param SystemAdminServices $services
     */
    public function __construct(App $app, SystemAdminServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 注释：获取菜单权限列表
     * 创建人：Jack
     * 创建时间：2023/10/26 16:10
     * @return mixed
     */
    public function get_menus()
    {
        return $this->success($this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']));
    }

    /**
     * 注释：index
     * 创建人：Jack
     * 创建时间：2023/7/31 11:18
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['name', '', '', 'username_like'],
            ['roles', ''],
            ['is_delete', 1],
            ['status', '']
        ]);
        $where['level'] = $this->adminInfo['level'] + 1;

        return $this->success($this->services->getAdminList($where));
    }

    /**
     * 注释：添加管理员
     * 创建人：Jack
     * 创建时间：2023/7/31 15:02
     * @return mixed
     */
    public function add()
    {
        $data = $this->request->postMore([
            ['username', ''],
            ['conf_password', ''],
            ['password', ''],
            ['true_name', ''],
            ['phone', ''],
            ['tel', ''],
            ['face', ''],
            ['lang', ''],
            ['email', ''],
            ['last_ip', ''],
            ['roles', []],
            ['dept_id', 0],
            ['status', 0],
            ['remarks', '']
        ]);

        $this->validate($data, \app\validate\admin\setting\SystemAdminValidate::class);

        $data['level'] = $this->adminInfo['level'] + 1;
        $this->services->create($data);
        return $this->success('添加成功!');
    }

    /**
     * 注释：read
     * 创建人：Jack
     * 创建时间：2023/7/31 15:53
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $user_info = $this->services->get($id);
        if (!$user_info) {
            return $this->fail('用户不存在!');
        }

        return $this->success(compact('user_info'));
    }

    /**
     * 修改管理员信息
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $data = $this->request->postMore([
            ['username', ''],
            ['conf_password', ''],
            ['password', ''],
            ['true_name', ''],
            ['phone', ''],
            ['tel', ''],
            ['email', ''],
            ['dept_id', 0],
            ['roles', []],
            ['face', ''],
            ['status', 0]
        ]);

        $this->validate($data, \app\validate\admin\setting\SystemAdminValidate::class, 'update');

        if ($this->services->save((int)$id, $data)) {
            return $this->success('修改成功!');
        } else {
            return $this->fail('修改失败!');
        }
    }


    /**
     * 注释：修改状态
     * 创建人：Jack
     * 创建时间：2023/7/31 11:45
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        $this->services->update((int)$id, ['status' => $status]);
        return $this->success($status == 0 ? '关闭成功!' : '开启成功!');
    }

    /**
     * 注释：删除管理员
     * 创建人：Jack
     * 创建时间：2023/7/31 11:43
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('删除失败，缺少参数!');
        if ($this->services->update((int)$id, ['is_delete' => 1, 'status' => 0])) {
            return $this->success('删除成功!');
        }

        return $this->fail('删除失败!');
    }

    /**
     * 注释：获取当前登陆管理员的信息
     * 创建人：Jack
     * 创建时间：2023/7/31 11:46
     * @return mixed
     */
    public function info()
    {
        return $this->success($this->adminInfo);
    }

    /**
     * 注释：修改当前登陆的管理员信息
     * 创建人：Jack
     * 创建时间：2023/7/31 11:46
     * @return mixed
     */
    public function update_admin()
    {
        $data = $this->request->postMore([
            ['true_name', ''],
            ['face', ''],
            ['password', ''],
            ['new_password', ''],
            ['conf_password', ''],
            ['remarks', ''],
            ['tel', ''],
            ['phone', ''],
            ['code', '']
        ]);
        if ($this->services->updateAdmin($this->adminId, $data)) {
            return $this->success('修改成功!');
        }

        return $this->fail('修改失败!');
    }

    /**
     * 注释：退出登陆
     * 创建人：Jack
     * 创建时间：2023/7/31 11:34
     * @return mixed
     */
    public function logout()
    {
        $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
        CacheService::redisHandler()->delete(md5($key));
        return $this->success('退出成功!');
    }

}